package com.dc.trans.steps.encryptsignature;

import java.util.List;
import java.util.Map;

import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;

import com.dc.trans.steps.decryptvalidatesign.DecryptValidateSign;
import com.dc.trans.steps.decryptvalidatesign.DecryptValidateSignMeta;

public class EncryptSignatureMeta extends BaseStepMeta implements StepMetaInterface{

	private static Class<?> PKG = DecryptValidateSignMeta.class; // for i18n purposes, needed by Translator2!!   $NON-NLS-1$
	private String stepName;
	private boolean isEncrypt;
	private boolean isSignature;
	private String encryptType;
	public String getEncryptType() {
		return encryptType;
	}

	public void setEncryptType(String encryptType) {
		this.encryptType = encryptType;
	}

	public String getStepName() {
		return stepName;
	}

	public boolean isEncrypt() {
		return isEncrypt;
	}

	public void setEncrypt(boolean isEncrypt) {
		this.isEncrypt = isEncrypt;
	}

	public boolean isSignature() {
		return isSignature;
	}

	public void setSignature(boolean isSignature) {
		this.isSignature = isSignature;
	}

	public void setStepName(String stepName) {
		this.stepName = stepName;
	}

	
	public EncryptSignatureMeta() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void check(List<CheckResultInterface> remarks, TransMeta transMeta,
			StepMeta stepMeta, RowMetaInterface prev, String[] input,
			String[] output, RowMetaInterface info) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public StepInterface getStep(StepMeta stepMeta,
			StepDataInterface stepDataInterface, int copyNr,
			TransMeta transMeta, Trans trans) {
		// TODO Auto-generated method stub
		return new EncryptSignature(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}

	@Override
	public StepDataInterface getStepData() {
		// TODO Auto-generated method stub
		return new EncryptSignatureData();
	}

	@Override
	public void loadXML(Node stepnode, List<DatabaseMeta> databases,
			Map<String, Counter> counters) throws KettleXMLException {
		// TODO Auto-generated method stub
		readData(stepnode, databases);
	}

	private void readData(Node stepnode, List<DatabaseMeta> databases) throws KettleXMLException {
		try
		{
			stepName       = XMLHandler.getTagValue(stepnode, "stepName"); //$NON-NLS-1$
			isEncrypt      = "Y".equalsIgnoreCase( XMLHandler.getTagValue(stepnode, "isEncrypt") );
			isSignature    = "Y".equalsIgnoreCase( XMLHandler.getTagValue(stepnode, "isSignature") );
			encryptType       = XMLHandler.getTagValue(stepnode, "encryptType");
		}
		catch(Exception e)
		{
			throw new KettleXMLException(BaseMessages.getString(PKG, "TransactionEndMeta.Exception.UnableToReadStepInfoFromXML"), e); //$NON-NLS-1$
		}
		
	}

	@Override
	public void readRep(Repository rep, ObjectId id_step,
			List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleException {
		try
		{
			stepName     = rep.getStepAttributeString(id_step, "stepName"); //$NON-NLS-1$
			isEncrypt        = rep.getStepAttributeBoolean(id_step, "isEncrypt");
			isSignature   = rep.getStepAttributeBoolean(id_step, "isSignature");
			encryptType     = rep.getStepAttributeString(id_step, "encryptType");
		}
		catch(Exception e)
		{
			throw new KettleException(BaseMessages.getString(PKG, "TransactionEndMeta.Exception.UnexpectedErrorReadingStepInfoFromRepository"), e); //$NON-NLS-1$
		}
		
	}

	@Override
	public void saveRep(Repository rep, ObjectId id_transformation,
			ObjectId id_step) throws KettleException {
		try
		{
			rep.saveStepAttribute(id_transformation, id_step, "stepName",stepName); //$NON-NLS-1$
			rep.saveStepAttribute(id_transformation, id_step, "isEncrypt",isEncrypt);
			rep.saveStepAttribute(id_transformation, id_step, "isSignature",isSignature);
			rep.saveStepAttribute(id_transformation, id_step, "encryptType",encryptType);
		}
		catch(Exception e)
		{
			throw new KettleException(BaseMessages.getString(PKG, "TransactionEndMeta.Exception.UnableToSaveStepInfoToRepository")+id_step, e); //$NON-NLS-1$
		}
		
	}

	@Override
	public void setDefault() {
		// TODO Auto-generated method stub
		
	}
	public String getXML()
	{
        StringBuilder retval = new StringBuilder(400);
		retval.append("    ").append(XMLHandler.addTagValue("stepName", stepName));
		retval.append("    ").append(XMLHandler.addTagValue("isEncrypt", isEncrypt));
		retval.append("    ").append(XMLHandler.addTagValue("isSignature", isSignature));
		retval.append("    ").append(XMLHandler.addTagValue("encryptType", encryptType));
		return retval.toString();
	}
}
